﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Other_LinkManage : BasePage
{
    t_linkBLL bll = new t_linkBLL();

    #region 属性
    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }
    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //base.CheckUserPermission("001002");
        if (!IsPostBack)
        {
            //绑定数据
            BindRepeaterData();
        }
    }
    #endregion

    #region 数据绑定
    private void BindRepeaterData()
    {
        UtilityBLL dbu = new UtilityBLL();

        //绑定数据源
        RepeaterList.DataSource = dbu.GetListByPage("t_link", "*", "1=1", "OrderNo", 0, 1, 40);
        RepeaterList.DataBind();
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label validText = (Label)e.Item.FindControl("ValidText");
            LinkButton Lbvalid = (LinkButton)e.Item.FindControl("BtnOperate");

            Lbvalid.Text = "修改";
            //设置启用禁用的显示文本
            if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Valid")) == 0)
            {
                validText.Text = "禁用";
                validText.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                validText.Text = "启用";
                validText.ForeColor = System.Drawing.Color.Green;
            }
        }
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Valid")
        {
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                t_link model = new t_link();
                model = bll.GetModel(id);
                model.Valid = model.Valid == 0 ? 1 : 0;
                bll.Update(model);
                BindRepeaterData();
            }
            catch (Exception ex)
            {
                base.Alert(ex.ToString());
            }
        }
        else if (e.CommandName == "Update")
        {
            try
            {
                Id = int.Parse(e.CommandArgument.ToString());
                Action = "Update";
                t_link model = new t_link();
                model = bll.GetModel(Id);
                this.txt_LinkName.Text = model.LinkName;
                this.txt_LinkURL.Text = model.LinkURL;
                this.txt_OrderNo.Text = model.OrderNo.ToString();

                this.lbtn_Submit.Text = "提交";
            }
            catch (Exception ex)
            {
                base.Alert(ex.ToString());
            }
        }
        else if (e.CommandName == "Delete")
        {
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                bll.Delete(id);
                BindRepeaterData();
            }
            catch (Exception ex)
            {
                base.Alert(ex.ToString());
            }
        }
    }
    #endregion

    #region 提交按钮
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        
        if (Action == "Update")
        {
            try
            {
                t_link model = new t_link();
                model = bll.GetModel(Id);
                model.LinkName = this.txt_LinkName.Text.Trim();
                model.LinkURL = this.txt_LinkURL.Text.Trim();
                model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                //model.Valid = 1;

                bll.Update(model);

                this.txt_LinkName.Text = "";
                this.txt_LinkURL.Text = "";
                this.txt_OrderNo.Text = "";
                this.lbtn_Submit.Text = "新增";
                Action = "Add";
                Id = 0;

                base.Alert("操作成功！");

                BindRepeaterData();
            }
            catch (Exception ex)
            {
            }
        }
        else
        {
            try
            {
                t_link model = new t_link();
                model.LinkName = this.txt_LinkName.Text.Trim();
                model.LinkURL = this.txt_LinkURL.Text.Trim();
                model.OrderNo = Convert.ToInt32(this.txt_OrderNo.Text.Trim());
                model.Valid = 1;

                bll.Add(model);

                this.txt_LinkName.Text = "";
                this.txt_LinkURL.Text = "";
                this.txt_OrderNo.Text = "";
                this.lbtn_Submit.Text = "新增";
                Action = "Add";
                Id = 0;

                base.Alert("操作成功！");

                BindRepeaterData();
            }
            catch (Exception ex)
            {
            }
        }
    }
    #endregion
}
